/**
 * 定时器ID
 */
var id;

/**
 * 保存
 * @param data
 * @param callback
 */
function savePonCaseVersionOffLine(data,callback){
	pdb.transaction(function (tx) {
    	tx.executeSql("INSERT INTO CASE_VERSION(" +
    			"CASE_VERSION_ID,GXDATE,ORGANID) " +
    			"VALUES (?,?,?)",
    			[data.id==undefined ? '' : data.id,
    			 data.gxDate==undefined ? '' : data.gxDate,
    	    			 data.organId==undefined ? '' : data.organId],
       function (tx, result) {
    	    if(typeof(callback) == 'function'){
    	    	callback(data);
    	    }
       },
	   function (tx, error) {
    	   pMessage("保存案件版本号失败");
	   });
    });
}


/**
 * 更新
 * @param data
 * @param callback
 */
function updatePonCaseVersionOffLine(data,callback){
	pdb.transaction(function (tx) {
    	tx.executeSql("UPDATE CASE_VERSION SET GXDATE=?,ORGANID=?",
    			[data.gxDate==undefined ? '' : data.gxDate,data.organId==undefined ? '' : data.organId],
       function (tx, result) {
    	    if(typeof(callback) == 'function'){
    	    	callback(data);
    	    }
       },
	   function (tx, error) {
    	   pMessage("更新案件版本号失败");
	   });
    });
}

/**
 * 通过ORGANID获取数据版本号
 * @param id 主键
 * @param callback 回调函数
 **/
function findPDataVersionByIdOffLine(){
//	$.extend($.mobile,{
//		loadingMessage: '正在加载...',
//	});
	
	setTimeout('showLoader("加载离线数据,请耐心等待...")', 100);
	
	findPonOrganById($.cookie('organId'),initPdata);
	/*db.transaction(function (tx) { 
		tx.executeSql("SELECT * FROM CASE_VERSION",[] , 
		function (tx, result){ 
		    var len = result.rows.length; 
		    if(len == 0){
		    	//getCaseVersion();
		    	getCaseVersion();
		    	return;
		    }
		    var caseVersion = {};
		    for (var i = 0; i < len; i++){
			    caseVersion.id = result.rows.item(i).CASE_VERSION_ID;
			    caseVersion.gxDate = result.rows.item(i).GXDATE;
			    caseVersion.organId = result.rows.item(i).ORGANID;
		    }
    	    getCaseVersion(caseVersion);
	    }, 
	    function(tx, ex) {
        });  
	});*/
}

function initPdata(data){
	if(data.id != undefined && data.id == $.cookie('organId')){
//		$.messager.progress('close');
		hideLoader();
		return;
	}
	getData();
}

/**
 * 获取案件系统数据
 */
function getData(){
	$.ajax({
		type : "POST",
		url : getContextPath() + "/case/getData",
		data : {
			userId:$.cookie("userId")
		},
		dataType : "json",
		success : function(data) {
			if(data.result == "success"){
				/**
				 * 同步用户表
				 **/
				ponUser(savePonUser,data.user);
				/**
				 * 同步机构执法门类表
				 **/
				ponOrganZfml(savePonOrganZfml,data.organZfml);
				/**
				 * 同步机构表
				 **/
				ponOrgan(savePonOrgan,data.organ);
				/**
				 * 同步部门表
				 **/
				ponDepart(savePonDepart,data.depart);
				/**
				 * 同步抽屉表
				 **/
				ponDrawer(savePonDrawer,data.drawer);
				/**
				 * 违法行为表
				 **/
				ponCaseCause(savePonCaseCauseOffLine,data.caseCause);
				/**
				 * 同步自由裁量权表
				 **/
				ponDiscretion(savePonDiscretionOffLine,data.discretion);
				/**
				 * 同步法条表
				 **/
				ponLawContent(savePonLawContentOffLine,data.lawContent);
				/**
				 * 同步案件编号表
				 **/
				ponCaseNumber(savePonCaseNumberOffLine,data.organCaseNumber);
				/**
				 * 同步人员表
				 **/
				ponStaff(savePonStaffOffLine,data.staff);
				/**
				 * 同步立案地址表
				 **/
				ponCaseFillingPlace(savePonCaseFillingPlaceOffLine,data.caseFillingPlace);
				/**
				 * 同步行政处罚种类
				 */
				ponCaseAdministrativePunishment(savePonAdministrativePunishmentOffLine,data.administrativePunishment);
				/**
				 * 查询数据
				 */
				id=setInterval("findPonLinkDocument()",2000); 
			}
		}
	});
}



function getPonCaseVersion(caseVersion){
	$.ajax({
		type : "POST",
		url : getContextPath() + "/case/getCaseVersion",
		dataType : "json",
		success : function(data) {
			if(data.result == "fail"){
				alert(data.errorMessage);
				return;
			}
			if(caseVersion == undefined){
				savePonCaseVersionOffLine(data.caseVersion);
			}
			
			if(caseVersion != undefined && data.caseVersion.gxDate == caseVersion.gxDate && $.cookie('organId') == caseVersion.organId){
//				$.messager.progress('close');
				return;
			}
			getData();
			updatePonCaseVersionOffLine(data.caseVersion);
		}
	});
}

function findPonLinkDocument(callback){
	pdb.transaction(function (tx) { 
		tx.executeSql("SELECT COUNT(*) TOTAL FROM CASE_LAW_CONTENT", [], function (tx, results){ 
			var len = results.rows.length;
			var count = 0;
		    for (var i = 0; i < len; i++){
		    	count = results.rows.item(i).TOTAL;
		    }
		    
			if(count){
				/**
				 * 取消定时执行
				 */
				clearInterval(id); 
				hideLoader();
			}
		    
			if (typeof(callback) == 'function'){
				callback(count);
			}
			
			return count;
	    }, 
	    function(tx, ex) {
	    	pMessage("获取与机构关联的环节失败");
        });
	});
}
